package com.android.test.model;

import java.util.ArrayList;

import android.content.Context;
import android.util.Log;

import com.android.test.util.DataRecordUtil;

public class Subject {
    private final static String TAG = "Subject";

    public final static String SUB_CHN = "chinese";
    public final static String SUB_MAT = "mathematics";
    public final static String SUB_ENG = "english";
    public final static String SUB_SYS = "system";

    private Context mContext = null;
    private String mSubject = null;
    private int mCurrentGrade = 0;

    private ArrayList<TestRecord> mHistory = new ArrayList<TestRecord>();

    public ArrayList<TestRecord> getResultAll() {
	try {
	    mHistory.clear();
	    mHistory = DataRecordUtil.getTestResultBySubjectAndGrade(mContext,
		    mSubject, -1, "week ASC");
	} catch (Exception e) {
	    return null;
	}
	return mHistory;
    }

    // Return all record of one subject of certain grade
    public ArrayList<TestRecord> getResultByGrade(int grade) {
	try {
	    mHistory.clear();
	    mHistory = DataRecordUtil.getTestResultBySubjectAndGrade(mContext,
		    mSubject, grade, "week ASC");
	} catch (Exception e) {
	    Log.e(TAG, "e=" + e.toString());
	    return null;
	}
	return mHistory;
    }

    public Subject(Context context, String subject) {
	mContext = context;
	mSubject = subject;
    }

    public static boolean chkSubject(String subject) {
	if (subject.equals(SUB_CHN) || subject.equals(SUB_MAT)
		|| subject.equals(SUB_ENG) || subject.equals(SUB_SYS)) {
	    return true;
	}
	return false;
    }

    public int getCurrentGrade() {
	return mCurrentGrade;
    }

    public void setCurrentGrade(int mCurrentGrade) {
	this.mCurrentGrade = mCurrentGrade;
    }

    public String getSubjectName() {
	return this.mSubject;
    }

    public int HashCode() {
	int result = 17;
	result = 31 * result + mSubject.length() + mSubject.charAt(0) * 17
		+ mSubject.charAt(mSubject.length() - 1);
	result = 31 * result + ((mContext == null) ? 0 : mContext.hashCode());
	Log.e("zzy","++++++++++" + result);
	return result;
    }
}